<template>
  <el-row class="login">
    <!--  底部  -->
    <el-row class="login-form-box">
      <el-col :span="12" class="height-100 left">
        <div class="logo">
          <img src="../../assets/images/login/logoUrl.png" alt />
        </div>
        <div class="title-left">
          <p>
            <span style="font-weight: normal">中国中铁广州轨道</span>
          </p>
          <p>
            <span>综合管理平台</span>
          </p>
        </div>
      </el-col>
      <el-col :span="12" class="height-100 right-form">
        <div class="form-title">登录</div>
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
          <el-form-item prop="username">
            <el-input
              v-model.trim="loginForm.username"
              type="text"
              auto-complete="off"
              placeholder="账号"
            >
              <img
                slot="prefix"
                src="../../assets/images/login/userName.png"
                class="input-icon"
              />
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model.trim="loginForm.password"
              type="password"
              auto-complete="off"
              placeholder="密码"
              @keyup.enter.native="handleLogin"
            >
              <img
                slot="prefix"
                src="../../assets/images/login/userPwd.png"
                class="input-icon"
              />
            </el-input>
          </el-form-item>
          <el-form-item prop="code">
            <el-input
              v-model.trim="loginForm.code"
              auto-complete="off"
              placeholder="图形验证码"
              style="width: 160px"
              @keyup.enter.native="handleLogin"
            >
              <img
                slot="prefix"
                src="../../assets/images/login/code.png"
                class="input-icon"
              />
            </el-input>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" />
            </div>
          </el-form-item>
          <el-checkbox
            v-model="loginForm.rememberMe"
            class="form-check"
            style="margin: 0px 0px 20px 0px"
          >
            记住密码
          </el-checkbox>
          <el-form-item style="width: 100%">
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              style="width: 100%; font-size: 18px"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <div class="el-login-footer">
      <span>
        技术支持：中铁一局集团有限公司智能科技分公司 建议使用 最新版本chrome 及
        IE11.0 以上浏览器 分辨率 1920x1080 及以上
      </span>
    </div>
  </el-row>
</template>

<script>
import Vue from 'vue'
import Cookies from 'js-cookie'
import { mapState, mapMutations } from 'vuex'
import { getCodeImg } from '@/services/login'
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default Vue.extend({
  name: 'login',
  data () {
    return {
      codeUrl: '',
      cookiePassword: '',
      loginForm: {
        username: '',
        password: '',
        rememberMe: false,
        code: '',
        uuid: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '用户名不能为空' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '密码不能为空' }
        ],
        code: [
          { required: true, trigger: 'change', message: '验证码不能为空' }
        ]
      },
      loading: false,
      redirect: undefined
    }
  },
  computed: {
    ...mapState({
      sidebar: state => state.app.sidebar
    })
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created () {
    this.getCode()
    this.getCookie()
    this.sidebar.opened && this['app/TOGGLE_SIDEBAR']()
  },
  methods: {
    ...mapMutations(['app/TOGGLE_SIDEBAR']),
    getCode () {
      getCodeImg().then((res) => {
        this.codeUrl = 'data:image/gif;base64,' + res.img
        this.loginForm.uuid = res.uuid
      })
    },
    getCookie () {
      const username = Cookies.get('username')
      const password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin () {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set('username', this.loginForm.username, { expires: 30 })
            Cookies.set('password', encrypt(this.loginForm.password), {
              expires: 30
            })
            Cookies.set('rememberMe', this.loginForm.rememberMe, {
              expires: 30
            })
          } else {
            Cookies.remove('username')
            Cookies.remove('password')
            Cookies.remove('rememberMe')
          }
          this.$store
            .dispatch('Login', this.loginForm)
            .then(() => {
              console.log(1111111111111, this.redirect || '/sysIndex/sysIndex')
              this.$router.push({ path: this.redirect || '/sysIndex/sysIndex' })
              window.sessionStorage.setItem('sidebarStatusKeep', true)
            })
            .catch(() => {
              this.loading = false
              this.getCode()
            })
        }
      })
    }
  }
})
</script>

<style rel="stylesheet/scss" lang="scss" scoped >
.el-checkbox__inner {
  border: 1px solid #ff5965;
  background-color: transparent;
}
.el-checkbox__input.is-checked .el-checkbox__inner {
  background-color: #ff0606;
  border-color: #ff0606;
}
.el-checkbox__input.is-focus .el-checkbox__inner {
  border: 1px solid #ff5965;
}
.el-button--primary {
  background-color: #ff0606;
  border-color: #ff0606;
}
.el-button--primary:hover,
.el-button--primary:focus {
  background-color: #ff0606;
  border-color: #ff0606;
}
// #FF5965
.login {
  height: 100%;
  background-image: url("../../assets/images/login/bg1.jpg");
  background-size: 100% 100%;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  .title {
    color: white;
    font-size: 2.25rem;
    font-weight: bold;
    display: flex;
    justify-content: center;
    align-items: center;
    img {
      padding-right: 15px;
    }
  }
  .login-form-box {
    height: 387px;
    width: 840px;
    background: white;
    position: absolute;
    left: 50%;
    top: 23%;
    transform: translateX(-50%);
    box-shadow: 0px 28px 43px 0px rgba(76, 123, 254, 0.22);
    border-radius: 0px 6px 6px 0px;
  }
  .input-icon {
    vertical-align: middle;
    width: 16px;
  }
  .left {
    height: 100%;
    background: url("../../assets/images/login/left_pic.png") no-repeat;
    background-size: 100% 100%;
    position: relative;
    .logo {
      position: absolute;
      left: 26px;
      top: 29px;
    }
    .title-left {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translateY(-50%) translateX(-50%);
      color: white;
      font-size: 36px;
      width: 100%;
      p {
        text-align: center;
      }
      span {
        &:first-child {
          font-weight: normal;
        }
        &:last-child {
          font-weight: bold;
        }
      }
    }
  }
  .right-form {
    padding: 28px 50px;
    .form-title {
      font-size: 28px;
      text-align: center;
      color: rgba(41, 39, 39, 1);
      margin-bottom: 23px;
    }
  }
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.el-form-item {
  margin-bottom: 16px;
}

.login-code {
  width: 129px;
  height: 42px;
  float: right;
  text-align: right;
  img {
    cursor: pointer;
    height: 35px;
    vertical-align: middle;
  }
}
.el-login-footer {
  background: rgba(0, 18, 54, 0.7);
  height: 50px;
  line-height: 50px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #a7e3ff;
  font-family: Arial;
  font-size: 1.5vmin;
  letter-spacing: 1px;
}

::v-deep .el-dialog__header {
  background-color: #fff;
}
::v-deep .el-dialog__title {
  color: #303133;
}
::v-deep .el-dialog__body {
  background: #fff;
}
::v-deep .el-dialog {
  border: 2px solid #fff;
}
::v-deep .el-dialog__headerbtn .el-dialog__close {
  color: #909399;
}
</style>
